﻿namespace MicroCloud.Systems
{
    public partial class DictService
    {
        /// <summary>
        /// 获取 登记实体查询数据集
        /// </summary>
        public IQueryable<DictEntry> DictEntrys => DictEntryRepository.QueryAsNoTracking();

        /// <summary>
        /// 根据编号查找
        /// </summary>
        /// <param name="id">数据实体编号</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task<OperationResult<DictEntryOutputDto>> FindDictEntryById(Guid id)
        {
            Check.NotEmpty(id, nameof(id));

            var outputDto = DictEntrys.Where(o => o.Id.Equals(id)).ToOutput<DictEntry, DictEntryOutputDto>().FirstOrDefault();
            if (outputDto != null)
            {
                return await Task.FromResult(new OperationResult<DictEntryOutputDto>(OperationResultType.Success, "OK", outputDto));
            }

            return await Task.FromResult(new OperationResult<DictEntryOutputDto>(OperationResultType.NotFound, "Not Found", outputDto));
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dtos">DTO信息</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task<OperationResult> CreateDictEntrys(params DictEntryInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            Check.Validate<DictEntryInputDto, Guid>(dtos, nameof(dtos));

            OperationResult result = await DictEntryRepository.InsertAsync(dtos);
            return result;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="dtos">DTO信息</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task<OperationResult> UpdateDictEntrys(params DictEntryInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            Check.Validate<DictEntryInputDto, Guid>(dtos, nameof(dtos));

            OperationResult result = await DictEntryRepository.UpdateAsync(dtos,
             async (dto, entity) =>
             {
                 Check.NotEmpty(dto.Id, nameof(dto.Id));

                 var dbEntity = DictEntryRepository.Get(dto.Id);
                 if (dbEntity == null)
                 {
                     result = new OperationResult(OperationResultType.Forbidden, $"编号为“{dto.Id}”的 DictData 不存在。");
                 }
                 await Task.FromResult(0);
             });
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids">数据实体编号</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task<OperationResult> DeleteDictEntrys(params Guid[] ids)
        {
            Check.NotNull(ids, nameof(ids));

            var result = OperationResult.Success();
            foreach (var id in ids)
            {
                Check.NotEmpty(id, nameof(id));

                var entity = DictEntryRepository.Get(id);
                if (entity == null)
                {
                    return new OperationResult(OperationResultType.Forbidden, $"编号为“{id}”的 DictData 不存在。");
                }
                result = await DictEntryRepository.DeleteAsync(entity);
                if (!result.Succeeded)
                {
                    return result;
                }
            }
            return result;
        }
    }
}
